package frame.project;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class Setting extends Activity{
	int[] arrBtn = {R.id.btnLabelRed, R.id.btnLabelOrange, R.id.btnLabelYellow,
			R.id.btnLabelGreen, R.id.btnLabelBlue, R.id.btnLabelPink, R.id.btnLabelPurple};
	
	Button btn;
	Button btnAboutUs,btnLabelRed,btnLabelOrange,btnLabelYellow,
	btnLabelGreen,btnLabelBlue,btnLabelPink,btnLabelPurple;
	
	DBAdapter db = new DBAdapter (this);
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        
        db.open();
        Cursor c = db.getLabel();
        Toast.makeText(getApplicationContext(), Integer.toString(c.getCount()), Toast.LENGTH_SHORT).show();
        for (int x = 0; x < arrBtn.length; x++){
        	btn = (Button) findViewById(arrBtn[x]);
        	btn.setText(c.getString(1).toString());
        	btn.setOnClickListener(new ListenerBtnClick(btn, x+1));
        	c.moveToNext();
        }
        
        db.close();
        
        
        //activate about us
        //opens about us class
        btnAboutUs = (Button) findViewById(R.id.btnAboutUs);
        btnAboutUs.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(),AboutUs.class);
				startActivity(i);
			}
		});  
    }
	
	public class ListenerBtnClick implements View.OnClickListener{
		Button button;
		int index;
		
		public ListenerBtnClick(Button btn, int x){
			this.button = btn;
			this.index = x;			
		}
		
		public void onClick(View v) {
			labelSetting(button, index);
		}
		
	}
	

	public void labelSetting(Button btn, int x){
		final Button button = btn;
		final int index = x;
		
		
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.dialog);
		dialog.setTitle("Label Name");
		
		
		
		TextView text = (TextView) dialog.findViewById(R.id.dialogText);
		
		final EditText input = (EditText) dialog.findViewById(R.id.dialogInput); 
		Button btnOk = (Button) dialog.findViewById(R.id.dialogOk);
		Button btnClear = (Button) dialog.findViewById(R.id.dialogClear);
		Button btnCancel = (Button) dialog.findViewById(R.id.dialogCancel);
		
		text.setText("New Label Name : ");
		input.setText(button.getText().toString());
		
		
		/*btnClear.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				input.setText("");
			}
		});*/
		btnOk.setOnClickListener(new ListenerDialogOk(button, index, input, dialog));
		btnClear.setOnClickListener(new ListenerDialogClear(input,"")); 
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		
		dialog.show();
	}
	
	public class ListenerDialogClear implements View.OnClickListener{
		EditText input;
		String text;
		
		public ListenerDialogClear(EditText in, String strText){
			this.input = in;
			this.text = strText;
		}
		
		public void onClick(View v) {
			input.setText(text);
		}
		
	}
	
	public class ListenerDialogOk implements View.OnClickListener{
		Button btn;
		int index;
		EditText input;
		Dialog dialog;
		
		public ListenerDialogOk(Button btnx,int x, EditText edit, Dialog d){
			this.btn = btnx;
			this.index = x;
			this.input = edit;
			this.dialog = d;
		}
		
		public void onClick(View v) {
			if (input.getText().toString().isEmpty()){
				Toast.makeText(getApplicationContext(), "Name can't be empty", Toast.LENGTH_SHORT).show();
				//btn.setText("Description..");
			}else{
				labelUpdate(btn,input.getText().toString(), index);
				btn.setText(input.getText().toString());	
				dialog.cancel();
			}
			
		}
		
	}
/*
public void labelSetting(Button btn, int x){
	//Create dialog box
	final Button button = btn;
	final int index = x;
	
	AlertDialog.Builder dialog = new AlertDialog.Builder(this);
	dialog.setTitle("Label Name");
	dialog.setMessage ("New label name");
	
	final EditText textAdd = new EditText(this);
	textAdd.setInputType(InputType.TYPE_CLASS_TEXT);
	dialog.setView(textAdd);
	
	dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			
			//check if text box empty
			if(textAdd.getText().toString().equals("")) {
				Toast.makeText(getApplicationContext(), "Name cannot be empty!", Toast.LENGTH_SHORT).show();
			}else {
				labelUpdate(button,textAdd.getText().toString(), index);
				Toast.makeText(getApplicationContext(), "New name is set!", Toast.LENGTH_SHORT).show();
			}
		}
	});
	
	dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			
		}
	});
	
	dialog.show();
	//...end of create dialog
  }
*/
public void labelUpdate(Button btn, String name, int index){
	db.open();
	Cursor c = db.getLabel();
	c.moveToPosition(index-1);
	db.updateLabel(index, name, c.getString(2).toString());
	db.close();
	btn.setText(name);
	}


}

